package com.health.adapter;

import java.util.HashMap;
import java.util.List;

import com.health.NovaApplication;
import com.health.R;
import com.health.db.FDbManager;
import com.health.fragment.jtys.JTYSDA;
import com.health.activity.qyinfo.QYInfo;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_DD_INFO;
import com.health.tools.GlobalConts;
import com.health.tools.PopData;
import com.health.widget.LabelsView;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 家庭医生档案listAdapter
 *
 * @author nova
 */
public class JTYSDAAdapter extends BaseAdapter {

    private List<ARCHIVE_BASEINFO> infoList;
    private Context context;
    private String type;
    private HashMap<Integer, View> viewMap = new HashMap<>();
    private GridView gd_da;

    public JTYSDAAdapter(Context context, List<ARCHIVE_BASEINFO> infoList, String type, GridView gd_da) {
        super();
        this.context = context;
        this.infoList = infoList;
        this.type = type;
        this.gd_da = gd_da;
    }

    public void setInfoList(List<ARCHIVE_BASEINFO> infoList) {
        this.infoList = infoList;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return null == infoList ? 0 : infoList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null==infoList?null:infoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (!viewMap.containsKey(position) || viewMap.get(position) == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.jtysda_item, null);

            holder.jtys_list_name = (TextView) convertView.findViewById(R.id.jtys_list_name);
            holder.jtys_list_age = (TextView) convertView.findViewById(R.id.jtys_list_age);
            holder.jtys_list_sex = (TextView) convertView.findViewById(R.id.jtys_list_sex);
            holder.jtys_list_phone = (TextView) convertView.findViewById(R.id.jtys_list_phone);
            holder.jtys_list_zdrq = (LabelsView) convertView.findViewById(R.id.jtys_list_zdrq);
            holder.jtys_list_address = (TextView) convertView.findViewById(R.id.jtys_list_address);
            holder.jtys_list_state_sign = (ImageView) convertView.findViewById(R.id.jtys_list_state_sign);
            holder.jtys_list_state_unsign = (ImageView) convertView.findViewById(R.id.jtys_list_state_unsign);
            convertView.setTag(holder);
            viewMap.put(position, convertView);

        } else {
            convertView = viewMap.get(position);
            holder = (ViewHolder) convertView.getTag();
        }

        if (viewMap.size() > 20) {
            synchronized (convertView) {
                for (int i = 1; i < gd_da.getFirstVisiblePosition() - 3; i++) {
                    viewMap.remove(i);
                }
                for (int i = gd_da.getLastVisiblePosition() + 3; i < getCount(); i++) {
                    viewMap.remove(i);
                }
            }
        }

        setData(holder, position, convertView);

        return convertView;
    }

    private class ViewHolder {

        TextView jtys_list_name;// 姓名
        ImageView jtys_list_img;// 头像
        TextView jtys_list_age;// 年龄
        TextView jtys_list_sex;
        TextView jtys_list_phone;// 手机号
        ImageView jtys_list_state_sign;// 签约状态
        ImageView jtys_list_state_unsign;// 签约状态
        LabelsView jtys_list_zdrq; // 重点人群
        TextView jtys_list_address;// 地址
    }

    /**
     * 设置数据
     *
     * @author 王婷婷
     * @date 2017年4月8日 上午9:30:07
     */
    private void setData(ViewHolder holder, int position, View convertView) {

        final ARCHIVE_BASEINFO info = infoList.get(position);
        Log.e("adapter---", info.toString());

        holder.jtys_list_name.setText(info.getCUSTOMERNAME());
        int age = GlobalConts.getAge(info.getBIRTHDAY());
        if (age != -1) {
            holder.jtys_list_age.setText(String.valueOf(age) + "岁");
        } else {
            holder.jtys_list_age.setText("");
        }
        //
        holder.jtys_list_state_unsign.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(context, QYInfo.class);
                changeUserInfoToAllData(info);
                context.startActivity(intent);

            }
        });

        List<BD_DD_INFO> bdxingbie = FDbManager.getInstance(context).findAllByWhereInInfoDB(BD_DD_INFO.class,
                "PARENTID = 'DM01-08'");
        String sex = "";
        for (BD_DD_INFO bdi : bdxingbie) {
            if (info.getSEX().equals(bdi.getID())) {
                sex = bdi.getDATAINFO();
            }
        }

        holder.jtys_list_sex.setText(sex);
        holder.jtys_list_phone.setText(info.getPHONE());

        PopData.setPopInfo(holder.jtys_list_zdrq, info, PopData.TAG_NORMAL, context);
//		setDataDemo(holder);
        holder.jtys_list_address.setText(info.getHOUSEHOLD_ADDR());

        if (type.equals(JTYSDA.TYPE_ALL)) {
            if (TextUtils.isEmpty(info.getDISTRICT_ID())) {
                holder.jtys_list_state_sign.setVisibility(View.GONE);
                holder.jtys_list_state_unsign.setVisibility(View.VISIBLE);
            } else {
                holder.jtys_list_state_sign.setVisibility(View.VISIBLE);
                holder.jtys_list_state_unsign.setVisibility(View.GONE);
            }

        } else if (type.equals(JTYSDA.TYPE_SIGN)) {
            holder.jtys_list_state_sign.setVisibility(View.VISIBLE);
            holder.jtys_list_state_unsign.setVisibility(View.GONE);
        } else if (type.equals(JTYSDA.TYPE_UNSIGN)) {
            holder.jtys_list_state_sign.setVisibility(View.GONE);
            holder.jtys_list_state_unsign.setVisibility(View.VISIBLE);
        }

    }

    private void changeUserInfoToAllData(ARCHIVE_BASEINFO info) {
        List<ARCHIVE_BASEINFO> idCardList = NovaApplication.fdbmanger.findAllByWhere(ARCHIVE_BASEINFO.class,
                "IDCARD = '" + info.getIDCARD() + "'");
        ARCHIVE_BASEINFO userinfo11 = idCardList.get(0);
        userinfo11.setDISTRICT_ID(info.getDISTRICT_ID());
        userinfo11.setIS_UPDATE("Y");
        NovaApplication.fdbmanger.updata(userinfo11);
        info = userinfo11;
        NovaApplication.setUserInfo(info);
    }


}
